/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorDrawPathPlugin = Class.create(FNISVGEditorPlugin, {
  initialize: function($super, editorInstance, pluginURL){
    $super(editorInstance, pluginURL);
    
    var button = new FNIToggleableButton({});
    button.setImage(this.getPluginURL() + "/gfx/icons/22x22/tools/draw-path.png");
    button.addListener('buttonUp', this, this._onButtonUp);
    button.addListener('buttonDown', this, this._onButtonDown);
    button.setTooltip(SVGLocale.getText("drawpathPlugin_buttonTooltip"));
    editorInstance.registerToolbarItem('drawPath', button);
    
    this._onKeyPressListener = this._onKeyPress.bindAsEventListener(this);
    Event.observe(window, "keydown", this._onKeyPressListener);
    this._drawing = false;
    this._addingPath = false;
  },
  getName: function(){
    return "drawpath";
  },
  _onButtonUp: function(event){
    this._stopListening();
  },
  _onButtonDown: function(event){
    this._startListening();
  },
  _startListening: function(){
    this.getEditorInstance().addListener("mousedragstart", this, this._onEditorMouseDragStart);
    this.getEditorInstance().addListener("mousedragend", this, this._onEditorMouseDragEnd);
    this.getEditorInstance().addListener("mousedragging", this, this._onEditorMouseDragging);
    this.getEditorInstance().addListener("mouseclick", this, this._onEditorMouseClick);
    this.getEditorInstance().addListener("mousemove", this, this._onEditorMouseMove);
    this.getEditorInstance().addListener("mouseup", this, this._onEditorMouseUp);
    this.getEditorInstance().addListener("doubleclick", this, this._onEditorDoubleClick);
  },
  _stopListening: function(){
    this.getEditorInstance().removeListener("mousedragstart", this);
    this.getEditorInstance().removeListener("mousedragend", this);
    this.getEditorInstance().removeListener("mousedragging", this);
    this.getEditorInstance().removeListener("mouseclick", this);
    this.getEditorInstance().removeListener("mousemove", this);
    this.getEditorInstance().removeListener("mouseup", this);
    this.getEditorInstance().removeListener("doubleclick", this);
  },
  _onEditorMouseDragStart: function(event){
  },
  _onEditorMouseDragEnd: function(event){
  },
  _onEditorMouseDragging: function(event){
  },
  _onKeyPress: function(event){
    if (event.keyCode == Event.KEY_ESC) {
      this._stopDrawing();
    }
  },
  _onEditorMouseClick: function(event){
    switch (event.button) {
      case Event.MOUSE_LEFT:
        if (this._drawing != true) {          
          this._drawingLastX = this._drawingStartX = event.mouseEditorX;
          this._drawingLastY = this._drawingStartY = event.mouseEditorY;
          
          this._tempElement = this.getEditorInstance().getSVGDocument().createElement('path');
          this._tempElement.setAttribute('style', 'stroke-dasharray:1,1; fill:none; fill-opacity: 0; stroke:#000000;stroke-width:1;stroke-opacity:1');
          var segment = new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('M', true, [{
            x: event.mouseEditorX,
            y: event.mouseEditorY
          }]);
                    
          var controller = getSVGElementControllerVault().getElementControllerFor(this._tempElement);
          controller.appendSegment(this._tempElement, segment);
          
          controller.updatePath(this._tempElement);
          
          this.getEditorInstance().getSVGDocument().appendTemporaryElement(this._tempElement);
          this._showGuideLine(event.mouseEditorX, event.mouseEditorY);
          this._drawing = true;
          this.getEditorInstance().getSelectionManager().setMode(FNISelectionManager.MODE_NONE);
        }
        else {
          var xLen = event.mouseEditorX - this._drawingLastX;
          var yLen = event.mouseEditorY - this._drawingLastY;
          
          var x1 = this._drawingLastX + ((xLen / 4) * 1);
          var y1 = this._drawingLastY + ((yLen / 4) * 1);
          var x2 = this._drawingLastX + ((xLen / 4) * 3);
          var y2 = this._drawingLastY + ((yLen / 4) * 3);
          var x = event.mouseEditorX;
          var y = event.mouseEditorY;
          
          var segment = new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('C', true, [{
            x: x1,
            y: y1
          }, {
            x: x2,
            y: y2
          }, {
            x: x,
            y: y
          }]);
          
          var controller = getSVGElementControllerVault().getElementControllerFor(this._tempElement);
          controller.appendSegment(this._tempElement, segment);
          controller.updatePath(this._tempElement);
          
          this._drawingLastX = event.mouseEditorX;
          this._drawingLastY = event.mouseEditorY;
          this._showGuideLine(event.mouseEditorX, event.mouseEditorY);
        }
      break;
    }
  },
  _onEditorMouseUp: function(event){
    switch (event.button) {
      case Event.MOUSE_RIGHT:
        if (this._drawing == true) {
          this._finalizeDrawing();
        }
      break;
    }
  },
  _onEditorDoubleClick: function(event) {
     switch (event.button) {
      case Event.MOUSE_LEFT:
        if (this._drawing == true) {
          this._finalizeDrawing();
        }
      break;
    }
  },
  _finalizeDrawing: function () {
    var element = this.getEditorInstance().createSVGElement('path');
    var controller = getSVGElementControllerVault().getElementControllerFor(this._tempElement);
    var xLenQ = (this._drawingStartX - this._drawingLastX) / 4;
    var yLenQ = (this._drawingStartY - this._drawingLastY) / 4;
    
    controller.appendSegment(this._tempElement, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('C', true, [{
      x: this._drawingLastX + (xLenQ * 1),
      y: this._drawingLastY + (yLenQ * 1)
    }, {
      x: this._drawingLastX + (xLenQ * 3),
      y: this._drawingLastY + (yLenQ * 3)
    }, {
      x: this._drawingStartX,
      y: this._drawingStartY
    }]));
    
    controller.appendSegment(this._tempElement, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('Z', true, []));
    controller.updatePath(this._tempElement);
    element.setAttribute('d', this._tempElement.getAttribute('d'));
    this.getEditorInstance().appendSVGElement(element);
    this._editorInstance.getUndoManager().addUndoableEvent(new FNISVGEditorUndoableElementAddEvent(element));
    this._stopDrawing();
  },
  _onEditorMouseMove: function(event){
     if (this._drawing == true) {
       this._moveGuideLine(event.mouseEditorX, event.mouseEditorY);
    }
  },
  _stopDrawing: function(){
    this.getEditorInstance().getSVGDocument().removeTemporaryElement(this._tempElement);
    this._tempElement = null;
    this._drawing = false;
    this._hideGuideLine();
    this.getEditorInstance().getSelectionManager().setPreviousMode();
  },
  _showGuideLine: function(x, y){
    if (!this._guideLine) {
      this._guideLine = this.getEditorInstance().getSVGDocument().createElement("line");
      this._guideLine.setAttribute('style', 'stroke-dasharray:1,1; fill:none; fill-opacity: 0; stroke:#000000;stroke-width:1;stroke-opacity:1');
      this.getEditorInstance().getSVGDocument().appendTemporaryElement(this._guideLine);
    }
    
    var controller = getSVGElementControllerVault().getElementControllerFor(this._guideLine);
    controller.setLine(this._guideLine, x, y, x, y);
  },
  _moveGuideLine: function(x, y){
    if (this._guideLine) {
      var controller = getSVGElementControllerVault().getElementControllerFor(this._guideLine);
      controller.setLine(this._guideLine, null, null, x, y);
    }
  },
  _hideGuideLine: function(){
    if (this._guideLine) {
      this.getEditorInstance().getSVGDocument().removeTemporaryElement(this._guideLine);
      this._guideLine = null;
    }
  }
});

document.fire("svgEditorPluginLoaded:drawpath", {
  pluginClass: FNISVGEditorDrawPathPlugin
});
